package com.nova.edc.trigger;

import java.util.Date;
import java.util.List;

import com.nova.edc.service.system.AreaPointService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.nova.edc.model.energy.CollectData;
import com.nova.edc.model.energy.Summary;
import com.nova.edc.service.collect.CloudConstant;
import com.nova.edc.service.collect.CloudDataService;
import com.nova.edc.service.energy.CollectDataService;
import com.nova.edc.service.energy.SummaryService;
import com.nova.edc.service.system.CollectAreaService;
import com.nova.frame.utils.DateUtils;
import com.seio.edc.util.DateTimeUtil;

public class TriggerGetData {

	private Logger log = Logger.getLogger(TriggerGetData.class);

	@Autowired
	private CloudDataService novaCloudDataService;
	@Autowired
	private CollectAreaService novaCollectAreaService;
	@Autowired
	private CollectDataService novaCollectDataService;
	@Autowired
	private SummaryService novaSummaryService;
	@Autowired
	private AreaPointService areaPointService;

	@Value("#{cloudDataProperties['history_start']}")
	private String historyStart;

	@Value("#{cloudDataProperties['history_end']}")
	private String historyEnd;

	public void execute() {

		log.info("云平台数据取得任务开始："
				+ DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
		String endTime = DateTimeUtil.addDate("minute", CloudConstant.BEFORE);
		String startTime = DateTimeUtil.addDate("minute", CloudConstant.BEFORE
				+ (0 - CloudConstant.SECTION));
		// 获取所有的网关
		List<String> gateways = areaPointService.getAllGateway();

		// 分别获取每个网关的所有数据，将数据插入到数据库中，并更新summary数据表
		for (int i = 0; i < gateways.size(); i++) {
			// 分别获取每个网关的所有数据，将数据插入到数据库中，并更新summary数据表
			List<CollectData> datas = novaCloudDataService
					.parseXml(novaCloudDataService.getXmlByDatetime(
							gateways.get(i), startTime, endTime));
			if (datas.size() > 0) {
				novaCollectDataService.insertAll(datas);
			}
			List<Summary> summaries = novaCloudDataService.parseSummary(
					gateways.get(i), startTime, endTime);
			if (summaries.size() > 0) {
				novaSummaryService.insertOrUpdateSummary(summaries);
			}
		}
		log.info("云平台数据取得任务结束："
				+ DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
	}

	public void executeHistory() {

		log.info("云平台历史数据取得任务开始："
				+ DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
		Date startDate = DateUtils.getStr2LDate(historyStart);
		Date endDate = DateTimeUtil.addDateMinute(startDate, 20);
		while (DateUtils.getStr2LDate(historyEnd).after(endDate)
				|| DateUtils.getStr2LDate(historyEnd).equals(endDate)) {

			String startTime = DateUtils.getDate2LStr(startDate);
			String endTime = DateUtils.getDate2LStr(endDate);
			// 获取所有的网关
			List<String> gateways = areaPointService.getAllGateway();

			// 分别获取每个网关的所有数据，将数据插入到数据库中，并更新summary数据表
			for (int i = 0; i < gateways.size(); i++) {
				// 分别获取每个网关的所有数据，将数据插入到数据库中，并更新summary数据表
				List<CollectData> datas = novaCloudDataService
						.parseXml(novaCloudDataService.getXmlByDatetime(
								gateways.get(i), startTime, endTime));
				if (datas.size() > 0) {
					novaCollectDataService.insertAll(datas);
				}
				List<Summary> summaries = novaCloudDataService.parseSummary(
						gateways.get(i), startTime, endTime);
				if (summaries.size() > 0) {
					novaSummaryService.insertOrUpdateSummary(summaries);
				}
			}
			startDate = endDate;
			endDate = DateTimeUtil.addDateMinute(startDate, 20);
		}

		log.info("云平台历史数据取得任务结束："
				+ DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
	}
}
